//
//  HWTakeALookPageHeader.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/8/3.
//  动态详情

import UIKit

class HWTakeALookPageHeader: UICollectionReusableView {
    var bgView:UIView!
    var headImageView:UIImageView!
    var headFrameImageView:UIImageView!
    var nameLabel:GranibleLabel!
    var timeLabel:UILabel!
    var contentLabel:UILabel!
//    var sexBgView:UIView!
//    var sexImgView:UIImageView!
//    var ageLabel:UILabel!
    var jumpToDetail:((_:HWGameCircleModel)->())?
    var jumpToUserDetail:((_:String)->())?
    var model:HWGameCircleModel?{
        didSet {
            headImageView.sd_setImage(with: URL(string: model?.face ?? ""), completed: nil)
            nameLabel.text = model?.pet_name
            contentLabel.text = model?.content
//            ageLabel.text = "model?.age"
            timeLabel.text = model?.time
            
//            if model?.gender == 1 {
//                sexImgView.image = UIImage(named: "manNumImage")
//                sexBgView.backgroundColor = .colorWithHexString(hexString: "#D6D4FF")
//                ageLabel.textColor = .colorWithHexString(hexString: "#6A63FC")
//            }else{
//                sexImgView.image = UIImage(named: "womenNumImage")
//                sexBgView.backgroundColor = .colorWithHexString(hexString: "#ECCAFF")
//                ageLabel.textColor = .colorWithHexString(hexString: "#BD50F9")
//            }
            
            headFrameImageView.sd_setImage(with: URL(string: model?.head_frame ?? ""), completed: nil)
            if model?.head_frame.count ?? 0 > 0{
                headImageView.snp.remakeConstraints { make in
                    make.left.equalTo(25)
                    make.top.equalTo(20)
                    make.width.height.equalTo(40)
                }
                nameLabel.snp.remakeConstraints { make in
                    make.left.equalTo(headFrameImageView.snp.right).offset(10)
                    make.top.equalTo(headImageView)
                }
                
                
            }else{
                headImageView.snp.remakeConstraints { make in
                    make.left.equalTo(15)
                    make.top.equalTo(10)
                    make.width.height.equalTo(40)
                }
                nameLabel.snp.remakeConstraints { make in
                    make.left.equalTo(headImageView.snp.right).offset(10)
                    make.top.equalTo(headImageView)
                }
                
            }

            if (model?.color.count ?? 0) > 1 {
                var arr:[UIColor] = Array()
                for color in model?.color ?? [] {
                    arr.append(UIColor.colorWithHexString(hexString: color))
                }
                nameLabel.colors = arr
            }else {
                nameLabel.colors = [UIColor.colorWithHexString(hexString: model?.color[0] ?? "#333333"),UIColor.colorWithHexString(hexString: model?.color[0] ?? "#333333")]
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWTakeALookPageHeader {
    private func initUI() -> Void {
        backgroundColor = .colorWithHexString(hexString: "#F9F9F9")
        bgView = UIView()
        bgView.backgroundColor = UIColor.white
        addSubview(bgView)
        
        headImageView = UIImageView()
        headImageView.backgroundColor = UIColor.clear
        headImageView.layer.cornerRadius = 20
        headImageView.layer.masksToBounds = true
        headImageView.isUserInteractionEnabled = true
        headImageView.contentMode = .scaleAspectFill
        bgView.addSubview(headImageView)
        
        headFrameImageView = UIImageView()
        headFrameImageView.backgroundColor = .clear
        headFrameImageView.layer.masksToBounds = true
        headFrameImageView.contentMode = .scaleAspectFit
        bgView.addSubview(headFrameImageView)
        
        nameLabel = GranibleLabel()
        nameLabel.font = .boldSystemFont(ofSize: 16)
        nameLabel.isUserInteractionEnabled = true
        bgView.addSubview(nameLabel)
        
        timeLabel = HWLabel.createLabel(title: "-", color: "#666666", font: 12)
        bgView.addSubview(timeLabel)
        
//        sexBgView = UIView.init()
//        sexBgView.backgroundColor = UIColor.colorWithHexString(hexString: "#ECCAFF")
//        sexBgView.layer.cornerRadius = 7
//        sexBgView.isUserInteractionEnabled = true
//        bgView.addSubview(sexBgView)
//
//        sexImgView = UIImageView.init()
//        sexImgView.image = UIImage(named: "womenNumImage")
//        sexBgView.addSubview(sexImgView)
//
//        ageLabel = HWLabel.createLabel(title: "-", color: "#BD50F9", font: 10)
//        sexBgView.addSubview(ageLabel)
        
        
        contentLabel = HWLabel.createLabel(title: "-", color: "#333333", font: 14)
        bgView.addSubview(contentLabel)
        
        
        let tapBg = UITapGestureRecognizer.init(target: self, action: #selector(tapBg))
        bgView.addGestureRecognizer(tapBg)
        
        let tapHeadImage = UITapGestureRecognizer.init(target: self, action: #selector(self.tapHeadImage))
        headImageView.addGestureRecognizer(tapHeadImage)
//        let tapName = UITapGestureRecognizer.init(target: self, action: #selector(self.tapHeadImage))
//        nameLabel.addGestureRecognizer(tapName)
//        let tapSex = UITapGestureRecognizer.init(target: self, action: #selector(self.tapHeadImage))
//        sexBgView.addGestureRecognizer(tapSex)
        
        
//        nameLabel.addGestureRecognizer(tapHeadImage)
    }
    
    private func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.right.bottom.equalTo(0)
            make.width.equalTo(screenWidth)
        }
        
        headImageView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.top.equalTo(10)
            make.width.height.equalTo(40)
        }
        
        headFrameImageView.snp.makeConstraints { make in
            make.center.equalTo(headImageView)
            make.height.width.equalTo(54)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(10)
            make.top.equalTo(headImageView)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        
//        sexBgView.snp.makeConstraints { make in
//            make.left.equalTo(nameLabel.snp.right).offset(15)
//            make.centerY.equalTo(nameLabel)
//            make.height.equalTo(14)
//        }
//
//        sexImgView.snp.makeConstraints { make in
//            make.left.equalTo(5)
//            make.top.equalTo(2)
//            make.bottom.equalTo(-2)
//        }
//
//        ageLabel.snp.makeConstraints { make in
//            make.left.equalTo(sexImgView.snp.right).offset(3)
//            make.right.equalTo(-5)
//            make.centerY.equalToSuperview()
//        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.top.equalTo(headImageView.snp.bottom).offset(11)
            make.right.equalTo(-15)
            make.bottom.equalTo(-10)
        }
    }
}
extension HWTakeALookPageHeader{
    
    @objc func tapBg() -> Void {
        jumpToDetail!(model ?? HWGameCircleModel())
    }
    
    @objc func tapHeadImage() -> Void {
//        jumpToUserDetail!(model?.live_uid ?? "")
    }
}
